package com.fbinggun.fattribute.attribute.attack;

import com.fbinggun.fattribute.FAttributePlugin;
import com.fbinggun.fattribute.attribute.AAttack;
import com.fbinggun.fattribute.effect.BuffEffect;
import com.fbinggun.fattribute.effect.IEffect;
import com.fbinggun.fattribute.data.DataManage;
import com.fbinggun.fattribute.data.EntityData;
import org.bukkit.entity.LivingEntity;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Map;

public abstract class BuffAttack extends AAttack {

    int duration=100;
    int modification=1;


    public PotionEffect getPotionEffect(){
        return new PotionEffect(getPotionEffectType(),getDuration(),getModification());
    }

    public void addPotionEffect(EntityData entityData){
        new BukkitRunnable() {
            @Override
            public void run() {
                entityData.getEntity().addPotionEffect(getPotionEffect());
            }
        }.runTask(FAttributePlugin.plugin);
    }

    public abstract PotionEffectType getPotionEffectType();

    public int getDuration() {
        return duration;
    }

    public int getModification() {
        return modification;
    }

    @Override
    public void getEffect(EntityData data, LivingEntity entity) {
        EntityData damageEntityData = DataManage.getData(entity);
        Map<IEffect,Integer> map =damageEntityData.getiEffects();
        if(map!=null&&map.size()!=0) {
            for (IEffect effect : map.keySet()) {
                if (effect instanceof BuffEffect) {
                    addPotionEffect(damageEntityData);
                    BuffEffect buffEffect= (BuffEffect) effect;
                    buffEffect.setDuration(100);
                    buffEffect.setModification(getValue(data));
                    data.iEffects.put(effect,getDuration());
                    return;
                }
            }
        }
        addPotionEffect(damageEntityData);
        damageEntityData.addEffect(new BuffEffect(getDuration(),getValue(data),data,this));
    }
}
